package uc108.com.jetpackdemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import uc108.com.jetpackdemo.R;
import uc108.com.jetpackdemo.observer.DefaultObserver;

public class LifecycleActivity extends Activity implements LifecycleOwner {
    LifecycleRegistry lifecycleRegistry;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lifecycleRegistry = new LifecycleRegistry(this);
        setContentView(R.layout.activity_lifecycle);
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return lifecycleRegistry;
    }

    public void bind(View view) {
        getLifecycle().addObserver(new DefaultObserver());
    }


    public void changeState(View view) {
        lifecycleRegistry.markState(Lifecycle.State.DESTROYED);
    }


    public void stateResume(View view) {

        lifecycleRegistry.markState(Lifecycle.State.RESUMED);
    }

    public void stateCreate(View view) {

        lifecycleRegistry.markState(Lifecycle.State.CREATED);
    }

    public void stateStart(View view) {

        lifecycleRegistry.markState(Lifecycle.State.STARTED);
    }

    public void stateInit(View view) {

        lifecycleRegistry.markState(Lifecycle.State.INITIALIZED);
    }

    public void getState(View view){
        Lifecycle.State state = lifecycleRegistry.getCurrentState();
        Log.e("LifeCycle","currentState " + state.name());
    }

}
